import apiService from "@service/index";

export const channelApiService = apiService.injectEndpoints({
    endpoints: (builder) => ({
        // 获取用户频道列表
        requestUserChannels: builder.query<ChannelResponse, undefined>({
            query: () => ({ url: "/user/channels" }),
            providesTags: ["userChannels" as never],
        }),
        // 获取访客频道列表
        // 由于访客频道列表不需要被自动缓存到 API Reducer 函数中
        // 所以此处使用 mutation 请求, mutation 请求不会缓存响应状态
        requestGuestChannels: builder.mutation<ChannelResponse, undefined>({
            query: () => ({ url: "/user/channels" }),
        }),
        // 删除用户频道
        deleteUserChannel: builder.mutation<null, number>({
            query: (id) => ({ url: `/user/channels/${id}`, method: "DELETE" }),
            invalidatesTags: ["userChannels" as never],
        }),
        // 获取所有频道列表
        requestAllChannels: builder.query<ChannelResponse, undefined>({
            query: () => ({ url: "/channels" }),
        }),
        // 添加用户频道
        addUserChannel: builder.mutation<AddChannelResponse, { channels: ChannelState[] }>({
            query: (body) => ({ url: "/user/channels", method: "PATCH", body }),
            invalidatesTags: ["userChannels" as never],
        }),
    }),
});

export const { useRequestGuestChannelsMutation, useRequestUserChannelsQuery, useDeleteUserChannelMutation, useRequestAllChannelsQuery,
    useAddUserChannelMutation,
} = channelApiService;